<?php

/**
 * @author 陈韵
 * 韩免Shopkorea创建订单
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Model\PssModel\OrderSupplier;
use Service\AppService\IdService;
use App\Service\DeclareService\WayBillDecareService;

class Shopkorea extends BaseSupplierRouterService 
{
    /**
     * 创建订单
     * @param array $param
     * @return array
     */
    public function createOrder($param = []) 
    {
        //下单商品重组
        $supplierGoodsInfo = $param['supplierGoodsInfo'];
        $userGoodsList     = $supplierGoodsInfo['userGoodsList'];
        $goodsList         = $supplierGoodsInfo['goodsList'];
        $orderItems        = [];
        $supplierCode      = $supplierGoodsInfo['supplierCode'];
        //按照仓区进行拆单 目前就一个仓库 不排除后面会有其它仓库
        $defaultStockGroupGoodsList = [];
        foreach ($goodsList as $item) {
            $defaultStockGroupGoodsList[$item['defaultStock']][] = $item;
            unset($item);
        }

        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
        ) ? true : false;
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false;

        foreach ($defaultStockGroupGoodsList as $item) {
            $currentGoods          = current($item);
            $supplierCommodityType = $currentGoods['supplierCommodityType'];

            //重新合并每一个仓库商品
            $orderGoods = [];
            foreach ($item as $goods) {
                $orderGoods = array_merge($orderGoods, $userGoodsList[$goods['skuCode']]);
                unset($goods);
            }

            //生成供应商主单号
            $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
            $param['order']['supplier_csn'] = IdService::instance()->getOtherID();

            //防止订单号重复问题,下游已申报，则使用下游的订单号作为中台子单号
            if ($isDelare && !$isShop && $supplierCommodityType != GOODS_TYPE_YBMY) {
                $param['order']['supplier_dsn'] = $param['order']['orderSn'];
            } else {
                $param['order']['supplier_dsn'] = IdService::instance()->getOtherID();
            }

            $param['order']['goods']          = $orderGoods;
            $param['order']['isQueryExpress'] = 2;
            $res                              = OrderSupplier::instance()->getSupplierCreateOrderData($param);
            if ($res['code'] != Normal::SUC) {
                return $res;
            }
            $orderItems[] = $res['data'];
        }

        return Normal::returnCode(Normal::SUC, "获取成功", [
            'supplierCode' => $supplierCode,
            'orderItems'   => $orderItems
        ]);
    }
    
    /**
     * 订单申报
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 获取物流信息
     * @param array $param
     */
    public function queryOrderAsyncExpressNotify($param = []) 
    {
        $queryBody = $param["queryBody"];
        $orderInfo = OrderSupplier::instance()->info(['id' => $queryBody['orderID'], 'company_id' => $queryBody['supplierID']]);
        if (empty($orderInfo)) {
            return Normal::returnCode(Normal::FAIL, "订单信息不存在");
        }
        $res = WayBillDecareService::instance()->queryOrderAsyncExpressNotify($orderInfo);
        if ($res['code'] != Normal::SUC ) {
            return Normal::returnCode(Normal::FAIL, $res['msg']);
        }
        
        $data = $res["data"]["data"] ?? [];
        if (empty($data)) {
            return Normal::returnCode(Normal::SUC, "供应商无发货数据");
        }
        $expressInfo = current($data);
        if (empty($expressInfo['logisticsNo'])) {
            return Normal::returnCode(Normal::FAIL, "获取物流单号logisticsNo失败");
        }
        if (empty($expressInfo['logisticsName'])) {
            return Normal::returnCode(Normal::FAIL, "获取物流公司logisticsName失败");
        }
        if ( empty($expressInfo['logisticsCode']) ) {
            return Normal::returnCode(Normal::FAIL, "获取物流公司代码logisticsCode失败");
        }
        $express_code = $expressInfo['logisticsCode'];
        $express_name = $expressInfo['logisticsName'];
        $express_sn   = $expressInfo['logisticsNo'];
        return Normal::returnCode(Normal::SUC, "已发货", [
            'express_name'      => mb_strtoupper($express_name, 'utf-8'),
            'express_code'      => $express_code,
            'express_sn'        => $express_sn,
        ]);
    }

    /**
     * 商品列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsList( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
}